package com.fangngng.pushall.controller.manage;

import com.fangngng.pushall.controller.BaseController;
import com.fangngng.pushall.dto.Result;
import com.fangngng.pushall.entity.Role;
import com.fangngng.pushall.entity.User;
import com.fangngng.pushall.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author fei.fang
 * @date 2021/4/13
 **/
@Slf4j
@RestController
public class UserRoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(value = "/role/add")
    public Result<Role> add(@RequestBody Role role) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return Result.fail("需要登陆");
        }

        return Result.ok(roleService.addRole(role));
    }

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(value = "/role/delete")
    public Result<Role> delete(@RequestBody Role role) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return Result.fail("需要登陆");
        }

        return Result.ok(roleService.deleteRole(role));
    }

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(value = "/role/edit")
    public Result<Role> edit(@RequestBody Role role) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return Result.fail("需要登陆");
        }

        return Result.ok(roleService.editRole(role));
    }

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping(value = "/role/page")
    public Result<List<Role>> subscribePost(@RequestBody Role role) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return Result.fail("需要登陆");
        }

        return Result.ok(roleService.page(1, 10, role));
    }

    // todo user role 增删改查
}
